<template>
  <div>
    <div class="bc">
      <div style="font: bolder 30px/2 normal;color: white">好看、好用、好玩、都在这里</div>
      <div class="search">
        <div>
          <el-input style="width: 350px;" placeholder="请搜索你想要的商品"/>
        </div>
        <div>
          <el-button type="primary" size="medium" prefix="el-icon-search">搜索</el-button>
        </div>
      </div>
    </div>

    <div style="display: flex;margin: 20px">
      <div style="width: 200px;"></div>
      <div style="flex: 1;">
        <el-card>
          <div slot="header" class="clearfix">
<!--            <el-select style="margin-right: 20px"
                       v-model="queryParams.category" placeholder="请选择分类">
              <el-option
                v-for="item in categoryList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>-->
            <el-select v-model="queryParams.type" placeholder="请选择分类">
              <el-option label="最热" value="hot"/>
              <el-option label="最新" value="new"/>
            </el-select>
          </div>
          <div style="min-height: 700px;">
            <el-row :gutter="20">
              <el-col :span="6" v-for="item in goodsList" :key="item.id" class="goods-item">
                <div class="card-wrapper" @click="$router.push('/goods/detail?id='+item.id)">
                  <image-preview
                    :src="item.img"
                    class="goods-image"
                    :width="245"
                    :height="245"
                  />
                  <div class="goods-info">
                    <div class="title line2">{{ item.name }}</div>
                    <div class="meta-container">
                      <div class="price">￥{{ item.price }}</div>
                      <div class="stats">
                        <span>👍 {{ item.likeCount }}</span>
                        <span><i class="el-icon-view"></i> {{ item.readCount }}</span>
                        <span><i class="el-icon-star-off"></i>{{ item.collectCount }}</span>
                      </div>
                    </div>
                  </div>
                </div>
              </el-col>
            </el-row>
          </div>

          <pagination
            v-show="total>0"
            :total="total"
            :page.sync="queryParams.pageNum"
            :limit.sync="queryParams.pageSize"
            @pagination="getList"
          />
        </el-card>
      </div>
      <div style="width: 200px;" class="right">
        <div>
          <el-button plain type="primary" @click="$router.push('/add/goods')">发布商品</el-button>
        </div>
        <div>
          <el-button plain type="primary" @click="$router.push('/add/help')">我要求购</el-button>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import {listGoods} from "@/api/project/goods";
import {listCategory} from "@/api/project/category";
import user from "@/store/modules/user";

export default {
  name: "home",
  computed: {
    userId() {
      return user.state.id
    }
  },
  created() {
    this.getList()
    listCategory({pageSize: 999}).then(res => this.categoryList = res.rows)
  },
  data() {
    return {
      goodsList: [],
      categoryList: [],
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        category: null,
        type: 'new'
      },
    }
  },
  watch: {
    'queryParams.type'() {
      this.goodsList = this.goodsList.reverse()
    },
  },
  methods: {
    getList() {
      listGoods(this.queryParams).then(res => {
        this.goodsList = res.rows
        this.total = res.total
      })
    },
  }
}
</script>
<style scoped lang="scss">
.line2 {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  text-overflow: ellipsis;
  min-height: 3em; /* 1.5em per line * 3 lines */
  line-height: 1.5em;
  flex: 1;
}

.goods-item {
  margin-bottom: 20px;
  padding: 0 10px;

  .card-wrapper {
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
    transition: transform 0.3s, box-shadow 0.3s;

    &:hover {
      transform: translateY(-5px);
      box-shadow: 0 4px 16px rgba(0, 0, 0, 0.15);
    }
  }

  .goods-image {
    border-radius: 8px 8px 0 0;
    display: block;
    object-fit: cover;
  }

  .goods-info {
    padding: 15px;
    background: white;

    .title {
      font-size: 14px;
      color: #333;
      line-height: 1.4;
      height: 40px;
    }

    .meta-container {
      margin-top: 12px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .price {
        color: #ff5722;
        font-size: 18px;
        font-weight: bold;
      }

      .stats {
        color: #999;
        font-size: 12px;

        span {
          margin-left: 10px;

          i {
            margin-right: 3px;
          }
        }
      }
    }
  }
}

.right {
  height: 100px;
  margin: 50px 0;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: center;
}

.bc {
  background-image: url("../../../assets/images/bc.jpg");
  height: 400px;
  background-size: cover;
  background-position: center;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.search {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}
</style>
